<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>信息</title>
		<script src="js/jquery.min.js"></script>
	</head>
	<body>
		<h2>请输入信息...</h2>
		<form id="form1">
			<table>
				<tr>
					<td>权限1</td>
					<input id="ID" name="ID" type="text" value="" style="display: none;" />
					<td><input name="ROLE_A_1" type="text" value="" /></td>
				</tr>
				<tr>
					<td>权限1</td>
					<td><input name="ROLE_A_2" type="text" value="" /></td>
				</tr>
				<tr>
					<td>权限2</td>
					<td><input name="ROLE_B_1" type="text" value="" /></td>
				</tr>
				<tr>
					<td>权限2</td>
					<td><input name="ROLE_B_2" type="text" value="" /></td>
				</tr><tr>
					<td>权限3</td>
					<td><input name="ROLE_C_1" type="text" value="" /></td>
				</tr>
				<tr>
					<td>权限3</td>
					<td><input name="ROLE_C_2" type="text" value="" /></td>
				</tr>
			</table>
			<input type="button" onclick="sub()" value="提交" />
		</form>
	</body>
	<script type="text/javascript">
		var name = "";
		//权限
		var a = "n";
		var b = "n";
		var c = "n";
		
		//页面加载前
		$(function() {
			//新增or更新
			var afterUrl = window.location.search.substring(1);
			console.log(afterUrl);
			if (afterUrl) {
				//回显ID
				var array = afterUrl.split("=");
				var data = array[1];
				$("#ID").val(data);
				//回显用户信息
				$.ajax({
					url: "http://localhost:8080/items/selectItemById?id=" + data,
					success: function(result) {
						if (result.status == 200) {
							let data = result.data;
							console.log(data);
							$("input[name='ROLE_A_1']").val(data.role_A_1);
							$("input[name='ROLE_A_2']").val(data.role_A_2);
							$("input[name='ROLE_B_1']").val(data.role_B_1);
							$("input[name='ROLE_B_2']").val(data.role_B_2);
							$("input[name='ROLE_C_1']").val(data.role_C_1);
							$("input[name='ROLE_C_2']").val(data.role_C_2);
							alert("更新");
						}
					}
				});
			} else {
				alert("新增用户")
			}

		})

		//加载完成后
		$(document).ready(function() {
			QUANXIAN();
		});
		
		//权限控制
		function QUANXIAN() {
			$.ajax({
				url: "http://localhost:8080/user/session",
				data: null,
				async: false,
				success: function(data) {
					console.log(data.data);
					//权限
					a = data.data.user_edit_top;
					b = data.data.user_edit_middle;
					c = data.data.user_edit_below;
					name = data.data.user_name;
					console.log(a, b, c, name);
					if (name) {
						alert(name)
						if(a =="n"){
							document.getElementsByName("ROLE_A_1")[0].readOnly=true;
							document.getElementsByName("ROLE_A_2")[0].readOnly=true;
						}
						if(b =="n"){
							document.getElementsByName("ROLE_B_1")[0].readOnly=true;
							document.getElementsByName("ROLE_B_2")[0].readOnly=true;
						}
						if(c =="n"){
							document.getElementsByName("ROLE_C_1")[0].readOnly=true;
							document.getElementsByName("ROLE_C_2")[0].readOnly=true;
						}
					}

				},
				error: function(e) {
					alert("出错");
					alert(e.responseText);
				},
			})
			// if ("n" == a || a == null) {
			// 	console.log(1)
			// 	document.getElementById("qx1").style.display = "none";
			// }
			// if ("n" == b || b == null) {
			// 	console.log(2)
			// 	document.getElementById("qx2").style.display = "none";
			// }
			// if ("n" == c || c == null) {
			// 	console.log(3)
			// 	document.getElementById("qx3").style.display = "none";
			// }
		}

		//提交
		function sub() {
			var id=$("#ID").val();
			console.log(id);
			let form = $("#form1");
			let FormData = form.serializeArray();
			console.log(FormData);
			let json1 = {};
			for (let i = 0; i < FormData.length; i++) {
				json1[FormData[i].name] = FormData[i].value;
			}
			json1["CREATE"] = name;
			console.log(json1);
			if (id != "") {
				// 更新
				$.ajax({
					url: "http://localhost:8080/items/update?ID="+id,
					data: json1,
					success: function(result) {
						if (result.status == 200) {
							alert(result.msg)
							// window.location.replace("http://localhost:8080/demo/admin.html")
							self.opener.location.reload();
							window.close();
						}
					}
				});
			} else{
				//新增
				$.ajax({
					url: "http://localhost:8080/items/add",
					data: json1,
					success: function(result) {
						if (result.status == 200) {
							alert(result.msg)
							// window.location.replace("http://localhost:8080/demo/admin.html")
							self.opener.location.reload();
							window.close();
						}
					}
				});
			}
		}

	</script>
</html>
